<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="van-nav-bar" fixed @click="$router.push('/search')">
      <template #title>
        <van-button
          icon="search"
          size="small"
          round
          class="search-btn"
          to="/search"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>
    <!-- 导航栏 -->
    <!-- 频道栏 -->
    <van-tabs v-model="active" animated swipeable class="channal-tabs">
      <van-tab :title="item.name" v-for="item in channel" :key="item.id">
        <ArticleList :channel="item" />
      </van-tab>
      <div slot="nav-right" class="btn"></div>
      <div slot="nav-right" class="hanber-btn" @click="isChannelShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道栏 -->
    <!-- 频道弹出层 -->
    <van-popup
      v-model="isChannelShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <ChannelEdit
        :mychannels="channel"
        :active="active"
        @update-active="change"
      />
    </van-popup>
    <!-- 频道弹出层 -->
  </div>
</template>

<script>
import ChannelEdit from '../home/component/channel-edit.vue'
import { getChannal } from '../../api/user'
import ArticleList from '../home/component/article-list.vue'
import { mapState } from 'vuex'
export default {
  name: 'homeIndex',

  data() {
    return {
      active: 0,
      channel: [],
      isChannelShow: false
    };
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  mounted() {
    this.getUserchannel()
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    async getUserchannel() {
      let channels = []
      try {
        if (this.user) {
          // 登录状态
          const { data } = await getChannal()
          channels = data.data.channels
        } else {
          // 未登录状态
          const result = JSON.parse(window.localStorage.getItem('toutiao_channel'))
          if (result) {
            channels = result
          } else {
            const { data } = await getChannal()
            channels = data.data.channels
          }
        }
      } catch (err) {
        this.$toast('获取用户频道列表失败！')
      }
      this.channel = channels
    },
    change(index, isChannelShow = true) {
      // console.log(index)
      this.active = index
      this.isChannelShow = isChannelShow
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-bottom: 100px;
  padding-top: 174px;
  .search-btn {
    width: 555px;
    height: 64px;
  }
  /deep/.channal-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 4;
    }
    .van-tab {
      // border-right: 1px solid #ccc;
      min-width: 200px;
      height: 80px;
    }
    .van-tab--active {
      color: red;
    }
    .hanber-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      border-color: #fff;
      opacity: 0.902;
      z-index: 99;
      .toutiao {
        font-size: 33px;
      }
    }
    .hanber-btn::before {
      content: '';
      position: absolute;
      left: 0;
      width: 3px !important;
      height: 100%;
      background-image: url('../../assets/gradient-gray-line.png');
      background-size: contain;
    }
    .btn {
      width: 66px;
      height: 82px;
    }
  }
}
</style>